<!--
 * @Description: 
 * @Version: 2.0
 * @Autor: wiz
 * @Date: 2024-07-23 09:51:08
 * @LastEditors: wiz
 * @LastEditTime: 2024-07-27 16:59:53
-->

<template>
  <div class="init-container">
    <div class="mask-container" v-if="isMask"></div>
    <div class="main">
      <div class="base-grid-container">
        <component
          :is="openCompName"
          v-bind="{
            config: bgConfig,
            regCustEventName: regCustEventName
          }"
          @on-cell-dblclick="onCellDblclick"
          @on-row-change="onRowChange"
          @mounted="onMounted"
        >
        </component>
      </div>
      <c-flowchart ref="graph" :config="openBpmnCf"></c-flowchart>
    </div>
  </div>
</template>

<script lang="ts">
import { N_oBaseGrid } from '@/types';
import { N_cFlowchart, T_srv, Tsrv, ifs, openBpmnConfig, vue } from './config';

@T_srv.comp
export default class openBpmn extends vue {
  /* 父组件compBoxInfo */
  @T_srv.prop({
    required: false,
    type: Object,
    default: null
  })
  compBoxInfo: ifs.I_compBoxInfo;

  /* 组件名 */
  private name: string = 'openBpmn';

  private funName: string = 'openBpmn';

  private G_compName: string = this.name;

  /* 组件箱信息对象 */
  private GT_compBoxInfo: ifs.I_compBoxInfo;

  /* 组件箱对象 */
  private GT_compBox: ifs.I_compBox;

  /* 组件箱识别ID */
  private G_compID: string;

  /* 遮罩 */
  public isMask: boolean = false;

  public openBpmnCf: N_cFlowchart.I_conf = null;

  public bgConfig: any = {};

  public openCompName: string = '';

  public regCustEventName: Array<N_oBaseGrid.I_regCustEventName | string> = [];

  private bgRef: any = null;

  created() {
    // 如果父组件没有传参，那就产生一个
    this.GT_compBoxInfo = Tsrv.getCompInfo(this.compBoxInfo);
    this.G_compID = this.GT_compBoxInfo.compID;
    this.GT_compBox = this.GT_compBoxInfo.TcompBox;
    this.init();
  }

  mounted() {
    /* 向父组件发送实例 */
    this.GT_compBox.addCompMergeName(this.G_compID, this.G_compName, this);
    this.$emit('compBoxInfo', this.GT_compBoxInfo);
  }

  destroyed() {
    /* 注册组件销毁 */
    this.GT_compBox.removeCompMergeName(this.G_compID, this.G_compName);

    /* 如果父组件传入的compBoxInfo则删除本文件产生的引用,反之是本组件产生的ID统一删除 */
    if (Tsrv.utils.isEmpty(this.compBoxInfo)) {
      this.GT_compBox.removeCompsWithCompID(this.G_compID);
    } else {
      this.GT_compBox.removeCompsMergeName(this.G_compID, [
        `'${this.G_compName}'`
      ]);
    }
  }

  private init() {
    this.funName = 'openBpmn'; // 当前配置中的功能名
    this.openBpmnCf = openBpmnConfig();

    this.getBgConfig().then(res => {
      if (res.isErr()) {
        throw new Error(res.errmsg);
      } else {
        const cf = res.getReturnData(Tsrv.globalVar.funName.BPMN_LIB);
        this.setCustDefListener(cf);
        this.bgConfig = cf;
        this.openCompName = Tsrv.globalVar.compName.BASE_GRID;
      }
    });
  }

  private getBgConfig(): Promise<ifs.I_return<any>> {
    return Tsrv.getFunConf(Tsrv.globalVar.funName.BPMN_LIB);
  }

  private setCustDefListener(cf) {
    const defListener = {
      receive: {
        source: cf.name,
        target: Tsrv.utils.custUUID(6) // 使接收事件无效
      }
    };
    const custDefListener = cf?.messageConfig?.custDefListener;
    if (!Tsrv.utils.noValue(custDefListener)) {
      let isNull = true;
      Object.keys(custDefListener).forEach(key => {
        if (!Tsrv.utils.noValue(custDefListener[key])) {
          isNull = false;
          if (Array.isArray(custDefListener[key])) {
            custDefListener[key].forEach(ele => {
              this.regCustEventName.push({
                receive: {
                  source: ele,
                  target: Tsrv.utils.custUUID(6) // 使接收事件无效
                }
              });
            });
          } else if (typeof custDefListener[key] === 'string') {
            this.regCustEventName.push(custDefListener[key]);
          } else {
            throw new Error(`无法识别的事件监听[${custDefListener[key]}]`);
          }
        }
      });
      if (isNull) {
        this.regCustEventName.push(defListener);
      }
    } else {
      // 拦截接收事件
      this.regCustEventName.push(defListener);
    }

    if (!Tsrv.utils.noValue(cf?.messageConfig)) {
      const emitActions = cf.messageConfig?.emitActions;
      // 发送事件拦截
      if (!Tsrv.utils.noValue(emitActions)) {
        emitActions.search.forEach(ele => {
          // 拦截发送事件
          this.regCustEventName.push({
            send: {
              source: ele.toID,
              target: Tsrv.utils.custUUID(6) // 使发送事件无效
            }
          });
        });
      }
    }
  }

  /* 设置组件整体遮罩 */
  public setMask(p: boolean) {
    this.isMask = p;
  }

  public getMask(p: boolean): boolean {
    return this.isMask;
  }

  onCellDblclick(e) {
    if (e?.row) {
      const row = Tsrv.utils.clone(e.row);
      const unzipResp = wUnZipStr(row[Tsrv.globalVar.fieldName.graphData]);
      if (unzipResp.isErr()) {
        throw new Error(unzipResp.getErrMsg());
      }
      row[Tsrv.globalVar.fieldName.graphData] = unzipResp.getReturnData('data');
      this.$emit('cell-dblclick', row);
    }
  }

  onRowChange(e) {
    if (e?.data?.to?.row) {
      const graph: any = this.$refs.graph;
      const unzipResp = wUnZipStr(
        e.data.to.row[Tsrv.globalVar.fieldName.graphData]
      );
      if (unzipResp.isErr()) {
        throw new Error(unzipResp.getErrMsg());
      }
      let d = unzipResp.getReturnData('data');
      if (!Tsrv.utils.isJSONString(d)) {
        d = JSON.stringify(d);
      }
      graph.reLoadDataFromJson(d);
    }
  }

  onMounted(e) {
    this.bgRef = e;
    this.$emit('bg-mounted', e);
  }
}
</script>
<style lang="scss" scoped>
.main {
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  .base-grid-container {
    width: 100%;
    height: 100%;
  }
}
</style>
